// pages/personal-coupon/personal-coupon.js

import api from '../../utils/request/api'
const util = require('../../utils/util.js');
import {
  userCoupon
} from '../../utils/request/apiurl'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: wx.getStorageSync('userInfo').uid, 
    topTab: [
      {
        title: "未使用",
        action: true
      },
      {
        title: "已使用",
        action: false
      },
      {
        title: "已过期",
        action: false
      }
    ],
    couponList: [],
    isRequest: false,
    searchGood: {
    	page: 1,
    	size: 10,
      pages: 0
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	 this.setData({
	 	uid: wx.getStorageSync('userInfo').uid,
	 	// role: wx.getStorageSync('userInfo').role
	 }) 
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	this.setData({
		uid: wx.getStorageSync('userInfo').uid,
		// role: wx.getStorageSync('userInfo').role
	})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */   
  onPullDownRefresh: function() {
  	wx.showNavigationBarLoading() //在标题栏中显示加载
	this.setData({
		searchGood: {
			page: 1,
			size: 10,
		  pages: 0
		},
	couponList:[]
	})
  	this.getList()
  	setTimeout(function() {
  		wx.hideNavigationBarLoading() //完成停止加载
  		wx.stopPullDownRefresh() //停止下拉刷新
  	}, 1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.isRequest && this.data.searchGood.page < this.data.searchGood.pages) {
    this.setData({
      searchGood: {
        page: this.data.searchGood.page + 1,
        size: 10,
        pages: this.data.searchGood.pages
      }
    })
    	this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 切换tag
  changeTab(e) {
    let index = e.currentTarget.dataset['index']
    this.data.topTab.map((a, nowIndex) => {
      a.action = false
      if (nowIndex === index) {
        a.action = true
      }
      return a
    })
    this.setData({
      topTab: this.data.topTab,
      couponList: [],
      searchGood: {
        page:  this.data.searchGood.page,
        size: this.data.searchGood.size,
        pages: this.data.searchGood.pages,
      },
    })
    this.getList(index)
  },
  //获取优惠券列表
  getList(type) {
    let nowType = 0
    if(type) {
      nowType = type
    }
    api.post(userCoupon.list, {
      type: nowType,
      page: this.data.searchGood.page,
      size: this.data.searchGood.size,
      uid:this.data.uid|| wx.getStorageSync('userInfo').uid,
	  // uid:'da3bdf61cc1a409cafe573ea389c0e09'
    }).then(res => {
      // console.log(res)
      let list = res.rows
      list.map(a => {
        a.time = util.getLocalTime(a.outTime)
        if(nowType === 0) {
          //未使用
          let dayTime = (a.outTime * 1000) - new Date().getTime()
          let dayLine = 3 * 24 * 60 * 60 * 1000
          if (dayTime < dayLine) {
            a.toExpire = true
          }
          // console.log(dayTime, dayLine)
        } else if(nowType === 1) {
          //已使用
          a.used = true
        } else if (nowType === 2) {
          //已过期
          a.expired = true
        }
        return a
      })
	  let pages = res.total / this.data.searchGood.size
      this.setData({
        couponList: [...this.data.couponList, ...list],
        pageData: {
          pages: pages,
          page: this.data.searchGood.page,
          size: this.data.searchGood.size,
        }
      })
      // console.log(this.data.couponList)
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  }
})